﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;

namespace OA.Controllers
{
    public class RechargeController : Controller
    {
        //
        // GET: /Recharge/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Recharge()
        {
            memberInfoManager manger = new memberInfoManager();
            memberInfo mInfo = new memberInfo();
            string memberId = System.Web.HttpContext.Current.Request.QueryString["memberId"];
            if (!string.IsNullOrEmpty(memberId))
            { 
                mInfo = manger.GetModel(memberId);
                if (mInfo.status != 0)
                {
                    return Content("<script>alert('此卡无法充值');window.parent.location.href='/Loading/Index/?src=/Desktop/Index/';parent.layer.close(parent.layer.getFrameIndex());</script>");
                }
                ViewData["memberId"] = mInfo.memberId;
                ViewData["point"] = mInfo.point;
                ViewData["balance"] =Convert.ToDecimal(mInfo.Balance).ToString("#0.00");
                ViewData["memberName"] = mInfo.memberName;
                return View();
            }
            if (Session["memberId"] != null)
            {
                mInfo = manger.GetModel(Session["memberId"].ToString());
                if (mInfo.status != 0)
                {
                    return Content("<script>alert('此卡无法充值');window.location.href='/Loading/Index/?src=/Desktop/Index/';</script>");
                }
                ViewData["memberId"] = mInfo.memberId;
                ViewData["point"] = mInfo.point;
                ViewData["balance"] = Convert.ToDecimal(mInfo.Balance).ToString("#0.00");
                ViewData["memberName"] = mInfo.memberName;
                return View();
            }
            return View();
        }
        public ActionResult RechargeMoney( FormCollection fc) 
        {
            memberInfoManager manger = new memberInfoManager();
            memberInfo mInfo = new memberInfo();
            string memberId = fc["memberId"];
            decimal balance =Convert.ToDecimal(fc["afterMoney"]);
            if (balance > 0)
            {
                if (manger.MemberRecharge(memberId, balance))
                {
                    //string memberId = manger.getMemberIdByMemberCardId(memberId);
                    string str = "alert('充值成功');window.location.href='/Recharge/Recharge/'";
                    Session["memberId"] = memberId;
                    return Content(str);
                }
                else
                {
                    return Content("alert('充值失败')");
                }
            }
            return Content("alert('充值金额应大于0')");
        }
    }
}
